using System.Collections;
using System.Collections.Generic;
using Framework.Library.Core.Config;
using Framework.Library.Core.Data.DataEngine;
using Framework.Library.Data.DAL;
using Framework.Library.Data.IDAO;
using Framework.Library.Core.Utils.Database;
using System.Data;
using System.Text;
using System;

namespace Framework.Library.Data.DAO.SqlServer
{
    [Serializable]
    internal class PortalPortalManager : IPortalPortalManager
    {
        private Configurations cfg = new Configurations();
        private QueryBuilder builder = new QueryBuilder("PORTAL_PORTALS");
        #region IPortalPortalManager Members
        
        public List<PortalPortal> GetPortalsByParentId(int userId, int portal_ParentId)
        {
            QueryBuilder builder = new QueryBuilder("PORTAL_PORTALS");
            builder.AddColumn("PortalID", "PORTAL_PORTALS");
            builder.AddColumn("Name", "PORTAL_PORTALS");
            builder.AddColumn("Portal_ParentId", "PORTAL_PORTALS");
            builder.AddTable("PORTAL_PORTALS", new List<string>() { "PORTALID" }, "PORTAL_ROLES", new List<string>() { "PORTALID" }, JoinTypes.InnerJoin);
            builder.AddTable("PORTAL_ROLES", new List<string>() { "ROLEID" }, "PORTAL_USERSROLES", new List<string>() { "ROLEID" }, JoinTypes.InnerJoin);
            builder.AddCondition("PORTAL_PARENTID", portal_ParentId, "PORTAL_PORTALS");
            builder.AddCondition("USERID", userId, "PORTAL_USERSROLES");
            var parameters = new ArrayList();
            parameters.Add(new object[] {"Portal_ParentId", portal_ParentId});
            parameters.Add(new object[] {"UserID", userId});
            return GeneralConnection.ExecuteList<PortalPortal>(CommandType.Text, builder);
        }

        public void Delete(int portalId)
        {
            QueryBuilder builder = new QueryBuilder("PORTAL_MENU");
            StringBuilder sb = new StringBuilder();
            sb.Append("BEGIN\t");
            sb.Append("UPDATE " + Configurations.DATABASEOWNER + "Portal_Portals SET Portal_ParentId = '0' where Portal_ParentId = @PortalID\t");
            sb.Append("delete " + Configurations.DATABASEOWNER + "PORTAL_PORTALS where PORTALID = @PortalID\t");
            sb.Append("delete " + Configurations.DATABASEOWNER + "PORTAL_MENU WHERE PORTALID = @PortalID\t");
            sb.Append("delete " + Configurations.DATABASEOWNER + "PORTAL_SECTIONS WHERE MEID not in (select ID from PORTAL_MENU) AND MEID <> 0\t");
            sb.Append("delete " + Configurations.DATABASEOWNER + "PORTAL_ROLES where PORTALID = @PortalID\t");
            sb.Append("delete " + Configurations.DATABASEOWNER + "PORTAL_CONTROLS where PORTALID = @PortalID\t");
            sb.Append("delete " + Configurations.DATABASEOWNER + "PORTAL_USERSPORTALS WHERE PORTALID = @PortalID\t");
            sb.Append("END\t");
            builder.Query = sb.ToString();
            builder.AddParameter("PortalID", portalId);
            builder.QueryType = QueryTypes.TSql;
            GeneralConnection.ExecuteNonQuery(CommandType.Text, builder);
        }

        #endregion
    }
}